<!--vue模板-->
<template>
    <el-row>
      <!-- 头部搜索 -->
      <el-form ref="form" :inline="true" :model="searchForm" class="form-inline">
        <el-form-item label="仓库名称" prop="name">
          <el-input v-model="searchForm.name" placeholder="仓库名称" />
        </el-form-item>
  
        <el-form-item label="仓库编码" prop="code">
          <el-input v-model="searchForm.code" placeholder="仓库编码" />
        </el-form-item>
  
        <el-form-item>
          <el-button type="primary" @click="onSubmit">搜索</el-button>
          <el-button @click="resetForm(form)">清空</el-button>
          <el-divider direction="vertical" border-style="solid" />
          <el-button type="primary" plain @click="visible = true">新增</el-button>
        </el-form-item>
      </el-form>
    </el-row>
    <el-row style="background-color: transparent; height: 80vh">
      <el-col :span="4">
        <CategoryTree ref="categoryRef"></CategoryTree>
      </el-col>
      <el-col :span="20" style="height: 100%">
        <el-table :data="tableData" border style="width: 100vw">
          <el-table-column prop="code" label="仓库编码" />
          <el-table-column prop="name" label="仓库名称" />
          <el-table-column prop="categoryName" label="所属类别" />
          <el-table-column prop="place" label="仓库地址" />
          
          <el-table-column label="操作">
            <template #default="scope">
              <el-button
                type="primary"
                :icon="Edit"
                circle
                @click="updateCategory(scope.row)"
              />
              <el-button
                type="danger"
                :icon="Delete"
                circle
                @click="deleteById(scope.row.id)"
              />
            </template>
          </el-table-column>
        </el-table>
      </el-col>
    </el-row>
  
    <SaveOrUpdate ref="saveUpdate" @close="refresh"></SaveOrUpdate>
  </template>
  
  <!--vue 组件选项定义-->
  <script setup>
  import { onBeforeMount, onMounted, provide, ref } from "vue";
  import CategoryTree from "./CategoryTree.vue";
  import SaveOrUpdate from "./SaveOrUpdate.vue";
  import { queryRepoAll,deleteRepository } from "@/api/basic/Repository.js";
  
  import { Delete, Edit } from "@element-plus/icons-vue";
  import { ElMessage } from "element-plus";
  
  const form = ref(null);
  
  
  const saveUpdate = ref(null);
  // 编辑数据
  const updateCategory = (row) => {
    visible.value = true;
    saveUpdate.value.init(row);
  };
  
  // 刷新整个界面的数据
  const refresh = () => {
     // 只显示一级分类
     categoryRef.value.changeData()
    getPepositoryAll();
  };
  
  const searchForm = ref({
    categoryCode: "",
    code: "",
    name: "",
  });
  
  // 新增 添加
  const visible = ref(false);
  
  provide("isShow", visible);
  
  const pidAdd = ref("");
  
  provide("pidAdd", pidAdd);
  
  // 清空form表单
  const resetForm = (formEl) => {
    formEl.resetFields();
    searchForm.value = {
      categoryCode: "",
      code: "",
      name: "",
    };
  
    getPepositoryAll()
  };
  
  // --------------
  //  查出的字典数据
  const tableData = ref([]);
  const categoryRef = ref(null);
  
  //-----------------
  const showCurData = async (id, data) => {
    console.log("datat",data);
    searchForm.value.categoryCode = data.code;
    const res = await queryRepoAll(searchForm.value);
    console.log("curData res",res);
    tableData.value = res.data;
  };
  
  provide("showCurData", showCurData);
  
  async function getPepositoryAll() {
    const res = await queryRepoAll(searchForm.value);
  
    tableData.value = res.data;
  }
  
  onMounted(() => {
    // 只显示一级分类
    categoryRef.value.changeData()
    getPepositoryAll();
  });
  
  // 搜索提交
  const onSubmit = () => {
    // console.log(formInline,"1234");
    getPepositoryAll();
    console.log("submit!");
  };
  
  const deleteById = (id) => {
    console.log("id");
  
    ElMessageBox.confirm("您将要删除当前用户，是否删除?", "警告", {
      confirmButtonText: "确定",
      cancelButtonText: "取消",
      type: "warning",
    })
      .then(() => {
        deleteRepository(id).then((resp) => {
          ElMessage.success("删除成功！");
          getPepositoryAll();
        });
      })
      .catch(() => {
        ElMessage({
          type: "info",
          message: "取消删除！",
        });
      });
  };
  </script>
  
  <!--vue样式 scoped当前样式只适用于当前组件-->
  <style scoped>
  :deep(.el-tree-node__content:hover) {
    background-color: aquamarine;
  }
  .el-row {
    margin-bottom: 20px;
  }
  .el-row:last-child {
    margin-bottom: 0;
  }
  .el-col {
    border-radius: 4px;
  }
  
  .grid-content {
    border-radius: 4px;
    min-height: 36px;
  }
  </style>
  